package com.coocaa.fileexplore;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.coocaa.fileexplore.pages.home.FileExploreHomeController;
import com.coocaa.fileexplore.pages.home.IHomeActivity;
import com.coocaa.fileexplore.pages.home.IHomeController;
import com.coocaa.fileexplore.pages.home.data.FileData;
import com.coocaa.fileexplore.utils.CommonUtils;
import com.coocaa.fileexplore.utils.FileUtils;

/**
 * Created by XuZeXiao on 2017/4/12.
 */

public class FileExploreHomeActivity extends Activity implements IHomeActivity {
    private LinearLayout mainLayout = null;
    private TextView currentUrl = null;
    private ListView fileList = null;
    private IHomeController controller = null;
    public static String TAG = "fileexplore";
    private String originUrl = "/data/data";
    private long extTime = 0L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        if (getIntent() != null && getIntent().getStringExtra("path") != null)
            originUrl = getIntent().getStringExtra("path");
        controller = new FileExploreHomeController();
        controller.setActivity(this);
        controller.refreshList(originUrl);
    }

    private void initView() {
        mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        ViewGroup.LayoutParams layoutParamsLayout = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        setContentView(mainLayout, layoutParamsLayout);

        currentUrl = new TextView(this);
        currentUrl.setTextSize(20);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.leftMargin = 20;
        mainLayout.addView(currentUrl, layoutParams);

        fileList = new ListView(this);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mainLayout.addView(fileList, layoutParams);

        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                FileData data = (FileData) adapterView.getItemAtPosition(i);
                if (data.name.equals("..")) {
                    loadParent(currentUrl.getText().toString());
                } else if (data.type == 0) {
                    controller.refreshList(data.url);
                }
            }
        });
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_BACK:
                    loadParent(currentUrl.getText().toString());
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    private void loadParent(String url) {
        //初始目录为根目录，不能访问初始目录的上级
        if (url.equals(originUrl)) {
            if (System.currentTimeMillis() - extTime > 2000) {
                Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                extTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return;
        }
        controller.refreshList(CommonUtils.getParentUrl(url));
    }

    @Override
    public void setUrl(String url) {
        if (url != null)
            currentUrl.setText(url);
    }

    @Override
    public void setAdapter(ArrayAdapter adapter) {
        if (fileList != null)
            fileList.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtils.scanFlag = false;
    }
}
